package com.carmanage.controller;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carmanage.service.ActionService;

@Controller
public class ActionController {
     
	
	@Resource
	private ActionService actionservice;

	public ActionService getActionservice() {
		return actionservice;
	}

	public void setActionservice(ActionService actionservice) {
		this.actionservice = actionservice;
	}
	
	
	@RequestMapping(value = "/findActionColumnByUserRole")
	public String findRoleActionColumn(HttpSession session){
        int userRoleId = (Integer) session.getAttribute("CurrentUserRoleId");
       TreeSet<Integer> TreeSetUserActionsId = actionservice.findActionidByRoleid(userRoleId);
		Set<Integer> SetUserActionColumnId = actionservice.findActionColumnId(TreeSetUserActionsId);
		session.setAttribute("CurrentUserActionsId",TreeSetUserActionsId);
		session.setAttribute("CurrentUserActionColumnid",SetUserActionColumnId);
		return "redirect:/pages/index.jsp";
	}
	
	@RequestMapping(value = "/findActionsIdByRoleId")
	public @ResponseBody List<Integer> findActionsIdByRoleId(int roleid){
		TreeSet<Integer> TreeSetUserActionsId = actionservice.findActionidByRoleid(roleid);
		Iterator<Integer> itera = TreeSetUserActionsId.iterator();
		List<Integer> ListActionId = new LinkedList<Integer>();
		while(itera.hasNext()){
           Integer actionId = (Integer) itera.next();
           ListActionId.add(actionId);
 		}
		return ListActionId;
	}
	
}
